package com.ruoyi.school.knowledge.controller;

import java.util.List;

import com.ruoyi.school.chapter.domain.Chapter;
import com.ruoyi.school.chapter.service.IChapterService;
import com.ruoyi.school.course.service.ICourseService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.school.knowledge.domain.Knowledge;
import com.ruoyi.school.knowledge.service.IKnowledgeService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 知识点管理Controller
 * 
 * @author qiang
 * @date 2022-06-26
 */
@Controller
@RequestMapping("/school/knowledge")
public class KnowledgeController extends BaseController
{
    private String prefix = "school/knowledge";

    @Autowired
    private IKnowledgeService knowledgeService;

    @Autowired
    private IChapterService chapterService;

    @Autowired
    private ICourseService courseService;

    @RequiresPermissions("school:knowledge:view")
    @GetMapping()
    public String knowledge()
    {
        return prefix + "/knowledge";
    }

    /**
     * 查询知识点管理列表
     */
    @RequiresPermissions("school:knowledge:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Knowledge knowledge)
    {
        startPage();
        List<Knowledge> list = knowledgeService.selectKnowledgeList(knowledge);
        return getDataTable(list);
    }

    /**
     * 导出知识点管理列表
     */
    @RequiresPermissions("school:knowledge:export")
    @Log(title = "知识点管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Knowledge knowledge)
    {
        List<Knowledge> list = knowledgeService.selectKnowledgeList(knowledge);
        ExcelUtil<Knowledge> util = new ExcelUtil<Knowledge>(Knowledge.class);
        return util.exportExcel(list, "知识点管理数据");
    }

    /**
     * 新增知识点管理
     */
    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        mmap.put("courses",courseService.selectCourseList(null));
        return prefix + "/add1";
    }

    /**
     * 新增保存知识点管理
     */
    @RequiresPermissions("school:knowledge:add")
    @Log(title = "知识点管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Knowledge knowledge,String chapterName)
    {
        //
        Chapter c = new Chapter();
        c.setName(chapterName);
        List<Chapter> list = chapterService.selectChapterList(c);
        if(list.size()>0)
        {
            knowledge.setChapterId(list.get(0).getId());
        }

        return toAjax(knowledgeService.insertKnowledge(knowledge));
    }

    /**
     * 修改知识点管理
     */
    @RequiresPermissions("school:knowledge:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Knowledge knowledge = knowledgeService.selectKnowledgeById(id);
        mmap.put("knowledge", knowledge);
        return prefix + "/edit1";
    }

    /**
     * 修改保存知识点管理
     */
    @RequiresPermissions("school:knowledge:edit")
    @Log(title = "知识点管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Knowledge knowledge)
    {
        return toAjax(knowledgeService.updateKnowledge(knowledge));
    }

    /**
     * 删除知识点管理
     */
    @RequiresPermissions("school:knowledge:remove")
    @Log(title = "知识点管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(knowledgeService.deleteKnowledgeByIds(ids));
    }
}
